<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <input type="text" placeholder="验证码倒计时"><button id="btn">验证码获取</button>
</body>
<script>
    var btn = document.getElementById("btn");

    // btn.onclick = sendCode;
    btn.addEventListener("click",sendCode);

    function sendCode(){
        btn.disabled = true;
        // btn.onclick = null;
        btn.removeEventListener("click",sendCode);
        var time = 20;

        var timer = setInterval(function(){
            if(time == 0){
                clearInterval(timer);
                btn.disabled = false;
                btn.innerHTML = "获取验证码"
                // btn.onclick = sendCode;
                btn.addEventListener("click",sendCode);
            }else{
                btn.innerHTML = time + "秒后重新发送"
                time--
            };
        },1000)
    }
</script>
</html>